//
//  GYTask.m
//  LockManagement
//
//  Created by 丁燕春 on 2018/8/5.
//  Copyright © 2018年 yunjiazhineng. All rights reserved.
//

#import "GYTask.h"
#import "GYNetWorkTools.h"
//#import "GYLock.h"
#import "GYTask+GYWCTTableCoding.h"
#import <WCDB/WCDB.h>
#import "NSString+GYPath.h"

@implementation GYTask

+ (void)getTakListsWithKeyID:(NSString *)keyID lockName:(NSString *)lockName taskType:(NSString *)taskType taskState:(NSString *)taskState success:(void (^)(NSArray *))success error:(void (^)(NSError *))failed {
    NSMutableArray *tempArray = [NSMutableArray array];
    NSString *IP = GETOBJ(@"IP")
    NSString *port = GETOBJ(@"port")
    [SVProgressHUD showWithStatus:@"正在加载任务列表..."];
    [GYNetWorkTools requestGetTasksListWithURL:[NSString stringWithFormat:@"http://%@:%@/api/getTaskKeyList",IP,port] lockName:lockName keyID:keyID taskType:taskType taskState:taskState success:^(NSDictionary *obj) {
        //NSLog(@"get task lists ========%@",obj);
        [SVProgressHUD dismiss];
        int state = [[obj objectForKey:@"state"] intValue];
        if (state == 0) {
            NSArray *data = [obj objectForKey:@"data"];
            for (NSDictionary *tempDic in data) {
                GYTask *task = [[GYTask alloc] initWithDic:tempDic];
                [tempArray addObject:task];
            }
        }
        success(tempArray);
    } error:^(NSError *error) {
        [SVProgressHUD showErrorWithStatus:@"加载任务列表失败,请稍后加载!"];
        failed(error);
        NSLog(@"get task list  error ====%@",error);
    }];
}

- (void)taskGive:(void (^)(BOOL))success error:(void (^)(NSError *))failed {
    NSString *IP = GETOBJ(@"IP")
    NSString *port = GETOBJ(@"port")
    [GYNetWorkTools requestTaskGiveWithURL:[NSString stringWithFormat:@"http://%@:%@/api/taskGive",IP,port] taskID:[NSString stringWithFormat:@"%d",self.taskID] success:^(NSDictionary *obj) {
        //NSLog(@"obj===========%@",obj);
        int state = [[obj objectForKey:@"state"] intValue];
        BOOL successed = NO;
        if (state == 0) {
            [SVProgressHUD showSuccessWithStatus:@"授权成功!"];
            successed = YES;
        }else {
            [SVProgressHUD showErrorWithStatus:@"授权失败!"];
        }
        success(successed);
    } error:^(NSError *error) {
        [SVProgressHUD showErrorWithStatus:@"授权失败!"];
        NSLog(@"task give error========%@",error);
    }];
}

- (instancetype)initWithDic:(NSDictionary *)dic {
    self = [super init];
    if (self) {
        [self setValuesForKeysWithDictionary:dic];
    }
    
    return self;
}

- (void)setValue:(id)value forKey:(NSString *)key {
    [super setValue:value forKey:key];
    if ([key isEqualToString:@"areas"]) {
        NSData *data = [NSJSONSerialization dataWithJSONObject:value options:NSJSONWritingPrettyPrinted error:nil];
        self.areas = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        
        return;
    }
}

- (void)setValue:(id)value forUndefinedKey:(NSString *)key {
    
}


#pragma mark -- WCDB 数据库操作

// 利用这个宏定义绑定到表的类
WCDB_IMPLEMENTATION(GYTask)

// 下面四个宏定义绑定到表中的字段

WCDB_SYNTHESIZE(GYTask, taskID)
WCDB_SYNTHESIZE(GYTask, taskName)
WCDB_SYNTHESIZE(GYTask, taskBegin)
WCDB_SYNTHESIZE(GYTask, taskEnd)
WCDB_SYNTHESIZE(GYTask, taskType)
WCDB_SYNTHESIZE(GYTask, taskState)
WCDB_SYNTHESIZE(GYTask, taskTime)
WCDB_SYNTHESIZE(GYTask, areas)
WCDB_SYNTHESIZE(GYTask, authName)
WCDB_SYNTHESIZE(GYTask, offineTime)

// 约束宏定义数据库的主键
WCDB_PRIMARY(GYTask, taskID)

// 定义数据库的索引属性，它直接定义createTime字段为索引
// 同时 WCDB 会将表名 + "_index" 作为该索引的名称
//WCDB_INDEX(Message, "_index", createTime)

+ (BOOL)createTaskTable {
    WCTDatabase *database = [self createDataBase];
            
    BOOL result = [database createTableAndIndexesOfName:@"task"
                                              withClass:GYTask.class];
    return result;
}

+ (void)insertTask:(GYTask *)task {
    WCTDatabase *database = [self createDataBase];
    [database insertObject:task into:@"task"];
}

+ (NSArray<GYTask *> *)getAllTaskData {
    WCTDatabase *database = [self createDataBase];
    NSArray<GYTask *> * task = [database getObjectsOfClass:GYTask.class fromTable:@"task" orderBy:GYTask.taskID.order()];
    
    return task;
}

+ (WCTDatabase *)createDataBase {
    // 文件路径
    NSString *filePath = [NSString DocumentDir:@"task.sqlite"];
    
    WCTDatabase *database = [[WCTDatabase alloc] initWithPath:filePath];
    
    return database;
}


@end
